package com.ymatou.iapi.godfreight.testcase;

import java.io.IOException;
import java.math.BigDecimal;

import org.apache.commons.lang3.tuple.Pair;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godfreight.parameter.getProductDefaultFreightBean;
import com.ymatou.iapi.godfreight.service.getProductDefaultFreightCall;
import com.ymatou.iapi.godfreightmgr.parameter.CreateFreightDetailDto;
import com.ymatou.iapi.godfreightmgr.parameter.CreateFreightTemplateBean;
import com.ymatou.iapi.godfreightmgr.parameter.EditFreightTemplateBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.GodfreightmgrService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.freight_template_detail;
import com.ymttest.database.sqlwapper.godfreight.freight_template_detailWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;
/**
*商品详细页显示商品默认运费
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getProductDefaultFreight{
	private static getProductDefaultFreightBean bean;
	private static getProductDefaultFreightCall call;
	private static freight_template_detailWapper detailWapper=new freight_template_detailWapper();
	private  int sellerId1 = AgentUsers.rootUserId;
	private  String sellerName1 = "NickautoTest";
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商品详细页显示商品默认运费");
	}
	@Before
	public void caseUp() throws Exception {
		bean=new getProductDefaultFreightBean();
		call=new getProductDefaultFreightCall();
		//渠道
		UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
		int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
		createmchBean createmchbean=GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
		GodmchManageCallService.createmchCallForChannel(createmchbean);
		sellerId1=userId_channl;
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	/**
	 * 
	 * @param firstNumOrWeight 首件/首重数量
	 * @param firstFee 续件/首重 数量
	 * @param addNumOrWeight 续增件/续增费
	 * @param addFee 续增费
	 * @param calcModel 计费方式 1 按件 2 按重
	 * @param sellerId 买家id
	 * @return 
	 * @throws NumberFormatException
	 * @throws IOException
	 */
	private Pair<Integer, Integer> setCreateFreightTemplate(BigDecimal firstNumOrWeight,BigDecimal firstFee,BigDecimal addNumOrWeight,BigDecimal addFee,int calcModel,int... sellerId) throws NumberFormatException, IOException
	{
		
		CreateFreightTemplateBean createBean=GodfreightmgrService.initCreateFreightTemplate(sellerId1,calcModel,1);
		if(sellerId.length>0) {
			createBean.setSellerId(sellerId[0]);
		}
		
		CreateFreightDetailDto createFreightDetailDto=createBean.getAgentFreight().getTemplateItems().get(0);
		if(calcModel==1) //计件
		{
			createFreightDetailDto.setFirstNum(Integer.parseInt(firstNumOrWeight.toString()));//首件数量
			createFreightDetailDto.setFirstFee(firstFee);//续件数量
			createFreightDetailDto.setAddNum(Integer.parseInt(addNumOrWeight.toString()));//续增件
			createFreightDetailDto.setAddFee(addFee);//续增费
		}
		//计重
		else if(calcModel==2) //计重
		{
			createFreightDetailDto.setFirstWeight(firstNumOrWeight);//首重数量
			createFreightDetailDto.setFirstFee(firstFee);//首续数量
			createFreightDetailDto.setAddWeight(addNumOrWeight);//首重运费
			createFreightDetailDto.setAddFee(addFee);// 续重 运费
		}
		
				
		String templateId =GodfreightmgrService.CreateFreightTemplate(createBean);
		Pair<Integer, Integer> templateIdAndSeller =Pair.of(Integer.parseInt(templateId),
				Integer.parseInt(String.valueOf(createBean.getSellerId())));
	    return templateIdAndSeller;
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductDefaultFreight_001() {
		Logger.start(true,"商品详细页显示商品默认运费单商品-按件-零售直销-[模版:首2件3元,每增1件2元]");
		try {
			//添加模版
			int templateId=this.setCreateFreightTemplate(new BigDecimal("2"),new BigDecimal("3"),new BigDecimal("1"),new BigDecimal("2"),1).getLeft();
			
			bean.setAppId(TestDataManager.getRandomNum(5));
			bean.setRequestId(TestDataManager.getRandomNum(5));
			bean.setProductId(TestDataManager.getRandomNum(5));
			bean.setSaleMode(1);//（1-零售直销，2-代理商销售）
			bean.setTemplateId(templateId);
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("true", call.getSuccess(), "验证返回Success成功");
			Logger.verifyEquals(bean.getRequestId(), call.getRequestId(), "验证返回RequestId");
			//查询freight_template_detail
			freight_template_detail model=detailWapper.selectDefaultByTemplateIdAndSaleMode(bean.getTemplateId(), bean.getSaleMode());
			Logger.verifyEquals(model.getFirstFee().stripTrailingZeros().toPlainString(), call.getdefaultFreight().stripTrailingZeros().toPlainString(), "验证返回defaultFreight");
			Logger.verifyEquals(model.getCurrency(), call.getCurrency(), "验证返回freightCurrency");
		
			//删除模版
			GodfreightmgrService.DeleteTemplateInfo(sellerId1,templateId) ;
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductDefaultFreight_002() {
		Logger.start(true,"商品详细页显示商品默认运费单商品-按重量-代理商销售-[商品1KG,模版:首1KG2元,每增1KG2元]");
		try {
			//添加模版
			int templateId=this.setCreateFreightTemplate(new BigDecimal("2"),new BigDecimal("3"),new BigDecimal("1"),new BigDecimal("2"),2).getLeft();
			
			bean.setAppId(TestDataManager.getRandomNum(5));
			bean.setRequestId(TestDataManager.getRandomNum(5));
			bean.setProductId(TestDataManager.getRandomNum(5));
			bean.setSaleMode(2);//（1-零售直销，2-代理商销售）
			bean.setTemplateId(templateId);
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("true", call.getSuccess(), "验证返回Success成功");
			Logger.verifyEquals(bean.getRequestId(), call.getRequestId(), "验证返回RequestId");
			//查询freight_template_detail
			freight_template_detail model=detailWapper.selectDefaultByTemplateIdAndSaleMode(bean.getTemplateId(), bean.getSaleMode());
			Logger.verifyEquals(model.getFirstFee().stripTrailingZeros().toPlainString(), call.getdefaultFreight().stripTrailingZeros().toPlainString(), "验证返回defaultFreight");
			

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductDefaultFreight_002_1() {
		Logger.start(true,"商品详细页显示商品默认运费单商品-saleMode=3");
		try {
			//添加模版
			int templateId=this.setCreateFreightTemplate(new BigDecimal("2"),new BigDecimal("3"),new BigDecimal("1"),new BigDecimal("2"),2).getLeft();
			
			bean.setAppId(TestDataManager.getRandomNum(5));
			bean.setRequestId(TestDataManager.getRandomNum(5));
			bean.setProductId(TestDataManager.getRandomNum(5));
			bean.setSaleMode(3);//（1-零售直销，2-代理商销售）
			bean.setTemplateId(templateId);
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("false", call.getSuccess(), "验证返回Success成功");
			Logger.verifyEquals(bean.getRequestId(), call.getRequestId(), "验证返回RequestId");	

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductDefaultFreight_002_3() {
		Logger.start(true,"商品详细页显示商品默认运费单商品-按重量-修改模版首重-[商品1KG,模版:首1KG2元,每增1KG2元]");
		try {
			//添加模版
			CreateFreightTemplateBean createBean=GodfreightmgrService.initCreateFreightTemplate(this.sellerId1,2,1);
			CreateFreightDetailDto createFreightDetailDto=createBean.getMainFreight().getTemplateItems().get(0);
			createFreightDetailDto.setFirstWeight(new BigDecimal("1"));//首重数量
			createFreightDetailDto.setFirstFee(new BigDecimal("2"));//首续数量
			createFreightDetailDto.setAddWeight(new BigDecimal("1"));//首重运费"
			createFreightDetailDto.setAddFee(new BigDecimal("2"));// 续重 运费
		
			int templateId =Integer.parseInt(GodfreightmgrService.CreateFreightTemplate(createBean));
			
			EditFreightTemplateBean eBean=GodfreightmgrService.initEditFreightTemplateBean(createBean,templateId);
			eBean.getMainFreight().getTemplateItems().get(0).setFirstWeight(new BigDecimal("2"));//首重数量
			eBean.getMainFreight().getTemplateItems().get(0).setFirstFee(new BigDecimal("3"));//首重数量
			
			GodfreightmgrService.EditFreightTemplate(eBean);
			
			bean.setAppId(TestDataManager.getRandomNum(5));
			bean.setRequestId(TestDataManager.getRandomNum(5));
			bean.setProductId(TestDataManager.getRandomNum(5));
			bean.setSaleMode(1);//（1-零售直销，2-代理商销售）
			bean.setTemplateId(templateId);
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("true", call.getSuccess(), "验证返回Success成功");
			Logger.verifyEquals(bean.getRequestId(), call.getRequestId(), "验证返回RequestId");
			//查询freight_template_detail
			freight_template_detail model=detailWapper.selectDefaultByTemplateIdAndSaleMode(bean.getTemplateId(), bean.getSaleMode());
			Logger.verifyEquals("3", call.getdefaultFreight().stripTrailingZeros().toPlainString(), "验证返回defaultFreight");
			Logger.verifyEquals(model.getCurrency(), call.getCurrency(), "验证返回Currency");
			

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductDefaultFreight_002_4() {
		Logger.start(true,"商品详细页显示商品默认运费单商品-按重量-修改模版续重-[商品3KG,模版:首2KG7元,每增4KG5元]");
		try {
			//添加模版
			CreateFreightTemplateBean createBean=GodfreightmgrService.initCreateFreightTemplate(this.sellerId1,2,1);
			CreateFreightDetailDto createFreightDetailDto=createBean.getMainFreight().getTemplateItems().get(0);
			createFreightDetailDto.setFirstWeight(new BigDecimal("1"));//首重数量
			createFreightDetailDto.setFirstFee(new BigDecimal("2"));//首续数量
			createFreightDetailDto.setAddWeight(new BigDecimal("1"));//续重运费"
			createFreightDetailDto.setAddFee(new BigDecimal("2"));// 续重 运费
		
			int templateId =Integer.parseInt(GodfreightmgrService.CreateFreightTemplate(createBean));
			
			EditFreightTemplateBean eBean=GodfreightmgrService.initEditFreightTemplateBean(createBean,templateId);
			eBean.getMainFreight().getTemplateItems().get(0).setFirstWeight(new BigDecimal("2"));//首重数量
			eBean.getMainFreight().getTemplateItems().get(0).setFirstFee(new BigDecimal("7"));//首重数量
			eBean.getMainFreight().getTemplateItems().get(0).setAddWeight(new BigDecimal("4"));//续重运费"
			eBean.getMainFreight().getTemplateItems().get(0).setAddFee(new BigDecimal("5"));// 续重 运费
			GodfreightmgrService.EditFreightTemplate(eBean);
			
			bean.setAppId(TestDataManager.getRandomNum(5));
			bean.setRequestId(TestDataManager.getRandomNum(5));
			bean.setProductId(TestDataManager.getRandomNum(5));
			bean.setSaleMode(1);//1-零售直销和代理, 2-直销代理共享
			bean.setTemplateId(templateId);
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("true", call.getSuccess(), "验证返回Success成功");
			Logger.verifyEquals(bean.getRequestId(), call.getRequestId(), "验证返回RequestId");
			//查询freight_template_detail
			freight_template_detail model=detailWapper.selectDefaultByTemplateIdAndSaleMode(bean.getTemplateId(), bean.getSaleMode());
			Logger.verifyEquals("7", call.getdefaultFreight().stripTrailingZeros().toPlainString(), "验证返回defaultFreight");
			Logger.verifyEquals(model.getCurrency(), call.getCurrency(), "验证返回Currency");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductDefaultFreight_002_5() {
		Logger.start(true,"商品详细页显示商品默认运费单商品-按件-修改模版首重-[商品3件,模版:首5件8元,每增6件2元]");
		try {
			//添加模版
			CreateFreightTemplateBean createBean=GodfreightmgrService.initCreateFreightTemplate(this.sellerId1,2,1);
			CreateFreightDetailDto createFreightDetailDto=createBean.getMainFreight().getTemplateItems().get(0);
			createFreightDetailDto.setFirstWeight(new BigDecimal("1"));//首重数量
			createFreightDetailDto.setFirstFee(new BigDecimal("2"));//首续数量
			createFreightDetailDto.setAddWeight(new BigDecimal("6"));//首重运费"
			createFreightDetailDto.setAddFee(new BigDecimal("2"));// 续重 运费
		
			int templateId =Integer.parseInt(GodfreightmgrService.CreateFreightTemplate(createBean));
			
			EditFreightTemplateBean eBean=GodfreightmgrService.initEditFreightTemplateBean(createBean,templateId);
			eBean.getMainFreight().getTemplateItems().get(0).setFirstWeight(new BigDecimal("5"));//首重数量
			eBean.getMainFreight().getTemplateItems().get(0).setFirstFee(new BigDecimal("8"));//首重数量
			
			GodfreightmgrService.EditFreightTemplate(eBean);
			
			bean.setAppId(TestDataManager.getRandomNum(5));
			bean.setRequestId(TestDataManager.getRandomNum(5));
			bean.setProductId(TestDataManager.getRandomNum(5));
			bean.setSaleMode(1);//（1-零售直销，2-代理商销售）
			bean.setTemplateId(templateId);
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("true", call.getSuccess(), "验证返回Success成功");
			Logger.verifyEquals(bean.getRequestId(), call.getRequestId(), "验证返回RequestId");
			//查询freight_template_detail
			freight_template_detail model=detailWapper.selectDefaultByTemplateIdAndSaleMode(bean.getTemplateId(), bean.getSaleMode());
			Logger.verifyEquals("8", call.getdefaultFreight().stripTrailingZeros().toPlainString(), "验证返回defaultFreight");
			Logger.verifyEquals(model.getCurrency(), call.getCurrency(), "验证返回Currency");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductDefaultFreight_002_6() {
		Logger.start(true,"商品详细页显示商品默认运费单商品-按重量-代理商销售--按件-修改模版货币类型和首重");
		try {
			//添加模版
			CreateFreightTemplateBean createBean=GodfreightmgrService.initCreateFreightTemplate(this.sellerId1,2,1);
			CreateFreightDetailDto createFreightDetailDto=createBean.getMainFreight().getTemplateItems().get(0);
			createFreightDetailDto.setFirstWeight(new BigDecimal("1"));//首重数量
			createFreightDetailDto.setFirstFee(new BigDecimal("2"));//首续数量
			createFreightDetailDto.setAddWeight(new BigDecimal("1"));//首重运费
			createFreightDetailDto.setAddFee(new BigDecimal("2"));// 续重 运费
		
			int templateId =Integer.parseInt(GodfreightmgrService.CreateFreightTemplate(createBean));
			
			EditFreightTemplateBean eBean=GodfreightmgrService.initEditFreightTemplateBean(createBean,templateId);
			eBean.setCurrency("USD");
			eBean.getMainFreight().getTemplateItems().get(0).setFirstWeight(new BigDecimal("2"));//首重数量
			eBean.getMainFreight().getTemplateItems().get(0).setFirstFee(new BigDecimal("3"));//首重数量
			
			GodfreightmgrService.EditFreightTemplate(eBean);
			
			bean.setAppId(TestDataManager.getRandomNum(5));
			bean.setRequestId(TestDataManager.getRandomNum(5));
			bean.setProductId(TestDataManager.getRandomNum(5));
			bean.setSaleMode(1);//（1-零售直销，2-代理商销售）
			bean.setTemplateId(templateId);
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("true", call.getSuccess(), "验证返回Success成功");
			Logger.verifyEquals(bean.getRequestId(), call.getRequestId(), "验证返回RequestId");
			//查询freight_template_detail
			freight_template_detail model=detailWapper.selectDefaultByTemplateIdAndSaleMode(bean.getTemplateId(), bean.getSaleMode());
			Logger.verifyEquals("3", call.getdefaultFreight().stripTrailingZeros().toPlainString(), "验证返回defaultFreight");
			Logger.verifyEquals("USD", call.getCurrency(), "验证返回Currency");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductDefaultFreight_003() {
		Logger.start(true,"商品详细页显示商品默认运费单商品-productId为空");
		try {
			//添加模版
			int templateId=this.setCreateFreightTemplate(new BigDecimal("2"),new BigDecimal("3"),new BigDecimal("1"),new BigDecimal("2"),2).getLeft();
			
			bean.setAppId(TestDataManager.getRandomNum(5));
			bean.setRequestId(TestDataManager.getRandomNum(5));
		//	bean.setProductId(TestDataManager.getRandomNum(5));
			bean.setSaleMode(2);
			bean.setTemplateId(templateId);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("false", call.getSuccess(), "验证返回Success成功");
			Logger.verifyEquals(bean.getRequestId(), call.getRequestId(), "验证返回RequestId");	

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductDefaultFreight_004() {
		Logger.start(true,"商品详细页显示商品默认运费单商品-saleMode为空");
		try {
			//添加模版
			int templateId=this.setCreateFreightTemplate(new BigDecimal("2"),new BigDecimal("3"),new BigDecimal("1"),new BigDecimal("2"),2).getLeft();
			
			bean.setAppId(TestDataManager.getRandomNum(5));
			bean.setRequestId(TestDataManager.getRandomNum(5));
			bean.setProductId(TestDataManager.getRandomNum(5));
			//bean.setSaleMode(2);
			bean.setTemplateId(templateId);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("false", call.getSuccess(), "验证返回Success成功");
			Logger.verifyEquals(bean.getRequestId(), call.getRequestId(), "验证返回RequestId");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductDefaultFreight_005() {
		Logger.start(true,"商品详细页显示商品默认运费单商品-templateId为空");
		try {
			//添加模版
			bean.setAppId(TestDataManager.getRandomNum(5));
			bean.setRequestId(TestDataManager.getRandomNum(5));
			bean.setProductId(TestDataManager.getRandomNum(5));
			bean.setSaleMode(2);
			//bean.setTemplateId(2);
			call.setData(bean);
			call.callService();			
			Logger.verifyEquals("false", call.getSuccess(), "验证返回Success成功");
			Logger.verifyEquals(bean.getRequestId(), call.getRequestId(), "验证返回RequestId");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getProductDefaultFreight_006() {
		Logger.start(true,"商品详细页显示商品默认运费单商品-验证运费币种freightCurrency");
		try {
			//添加模版
			int templateId=this.setCreateFreightTemplate(new BigDecimal("2"),new BigDecimal("3"),new BigDecimal("1"),new BigDecimal("2"),1).getLeft();
			
			bean.setAppId(TestDataManager.getRandomNum(5));
			bean.setRequestId(TestDataManager.getRandomNum(5));
			bean.setProductId(TestDataManager.getRandomNum(5));
			bean.setSaleMode(1);//（1-零售直销，2-代理商销售）
			bean.setTemplateId(templateId);
			
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("true", call.getSuccess(), "验证返回Success成功");
			Logger.verifyEquals(bean.getRequestId(), call.getRequestId(), "验证返回RequestId");
			//查询freight_template_detail
			freight_template_detail model=detailWapper.selectDefaultByTemplateIdAndSaleMode(bean.getTemplateId(), bean.getSaleMode());
			Logger.verifyEquals(model.getFirstFee().stripTrailingZeros().toPlainString(), call.getdefaultFreight().stripTrailingZeros().toPlainString(), "验证返回defaultFreight");
			Logger.verifyEquals(model.getCurrency(), call.getCurrency(), "验证返回freightCurrency");
		
			//删除模版
			GodfreightmgrService.DeleteTemplateInfo(sellerId1,templateId) ;
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}